'use strict';

/**
 * @ngdoc function
 * @name webApp.controller:MaintextCtrl
 * @description
 * # MaintextCtrl
 * Controller of the webApp
 */

angular.module('webApp')
  .controller('mainText', function ($scope,$rootScope,promiseMainText,cookie,$ionicScrollDelegate,$filter,$location,$ionicLoading) {
    if($location.search()['id'] && !cookie.getCookie('newsId')){
      cookie.setCookie('newsId',$location.search()['id'].replace(/%3Fid/g,''));
    }
    //正文
    var filter = _.memoize(function(data){
      return $filter('trustHtml',data);
    })();
    $scope.$on('$stateChangeSuccess',function() {
      $scope.mainText = {};
      if(cookie.getCookie('newsId') && $location.$$path == '/root/mainText'){
        $ionicLoading.show({
          template: '加载中...'
        });
        promiseMainText.articleMain(cookie.getCookie('newsId')).then(function(data){
          if(data.article.content.indexOf('swf') == -1){
            data.article.content = filter(data.article.content);
            data.article.title = filter(data.article.title);
            data.article.origin = filter(data.article.origin);
            $scope.mainText = data;
          }
          else{
            data.article.content = '<h2 style="text-align: center">手机不支持此类文件，请去PC端打开。</h2>';
            data.article.content = filter(data.article.content);
            data.article.title = filter(data.article.title);
            data.article.origin = filter(data.article.origin);
            $scope.mainText = data;
          }
          $scope.$emit('star',{data: data.article.is_starred});
          $ionicScrollDelegate.resize();
          //分享sdk
          mobShare.config({
            appkey: 'b0211f848370',
            params: {
              url: location.href + '?id=' + cookie.getCookie('newsId'),
              title: '上海国际航运研究中心：'  + data.article.title,
              description: '',
              pic: data.article.thumbnail_url
            }
          });
          $ionicLoading.hide();
        });
      }
      $scope.relatedId = function(id){
        cookie.setCookie('newsId',id);
        if(cookie.getCookie('newsId')){
          promiseMainText.articleMain(cookie.getCookie('newsId')).then(function(data){
            data.article.content = filter(data.article.content);
            data.article.title = filter(data.article.title);
            data.article.origin = filter(data.article.origin);
            $scope.mainText = data;
            $scope.$emit('star',{data: data.article.is_starred});
            $ionicScrollDelegate.resize();
            $ionicScrollDelegate.scrollTop(false);
          });
        }
      };
    });
    //监听评论消息
    $rootScope.$on('/root/mainText',function(ele,data){
      if(data.action == '写评论'){
        if($scope.mainText.hot_comments){
          $scope.mainText.hot_comments.splice(0,0,data.data);
        }
        else{
          $scope.mainText.hot_comments = [data.data];
        }
      }
      else if(data.action == '写回复'){
        for(var i = 0;i < $scope.mainText.hot_comments.length;i++){
          if($scope.mainText.hot_comments[i].id == cookie.getCookie('replayId')){
            if($scope.mainText.hot_comments[i].replies){
              $scope.mainText.hot_comments[i].replies.splice(0,0,data.data);
            }
            else{
              $scope.mainText.hot_comments[i].replies = [data.data];
            }
          }
        }
      }
    });
});
